package com.itheima.travel.web;

import com.itheima.travel.enums.StatusEnum;
import com.itheima.travel.exception.ProjectException;
import com.itheima.travel.req.SellerVo;
import com.itheima.travel.res.ResponseWrap;
import com.itheima.travel.service.SellerService;
import com.itheima.travel.utils.ExceptionsUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("seller")
@Log4j2
@Api(tags = "商家用户操作")
@CrossOrigin
public class SellerController {

    @Autowired
    SellerService sellerService;

    @PostMapping("register")
    @ApiOperation(value = "商家用户注册",notes = "商家用户注册")
    @ApiImplicitParam(name = "sellerVo",value = "注册信息",required = true,dataType = "SellerVo")
    public ResponseWrap<Boolean> registerSeller(@RequestBody SellerVo sellerVo) throws ProjectException {
        try {
            Boolean flag = sellerService.registerSeller(sellerVo);
            return ResponseWrap.<Boolean>builder()
                    .code(StatusEnum.SUCCEED.getCode())
                    .msg(StatusEnum.SUCCEED.getMsg())
                    .operationTime(new Date())
                    .data(flag)
                    .build();
        }catch (Exception e){
            log.error("商家用户注册：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(StatusEnum.REGISTER_USER_FAIL.getCode(),
                    StatusEnum.REGISTER_USER_FAIL.getMsg());
        }

    }

    @PostMapping("login")
    @ApiOperation(value = "商家用户登录",notes = "商家用户登录")
    @ApiImplicitParam(name = "sellerVo",value = "登录信息",required = true,dataType = "SellerVo")
    public ResponseWrap<SellerVo> loginSeller(@RequestBody SellerVo sellerVo) throws ProjectException {
        try {
            SellerVo sellerVoResult = sellerService.loginSeller(sellerVo);
            return ResponseWrap.<SellerVo>builder()
                    .code(StatusEnum.SUCCEED.getCode())
                    .msg(StatusEnum.SUCCEED.getMsg())
                    .operationTime(new Date())
                    .data(sellerVoResult)
                    .build();
        }catch (Exception e){
            log.error("商家用户登录：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(StatusEnum.LOGIN_USER_FAIL.getCode(),
                    StatusEnum.LOGIN_USER_FAIL.getMsg());
        }

    }

    @GetMapping("logout")
    @ApiOperation(value = "用户退出",notes = "用户退出")
    public ResponseWrap<Boolean> loginOutUser() throws ProjectException {
        Boolean flag = true;
        try {
            sellerService.loginOutSeller();
            return ResponseWrap.<Boolean>builder()
                    .code(StatusEnum.SUCCEED.getCode())
                    .msg(StatusEnum.SUCCEED.getMsg())
                    .operationTime(new Date())
                    .data(flag)
                    .build();
        }catch (Exception e){
            log.error("是否登录：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(StatusEnum.FAIL.getCode(),
                    StatusEnum.FAIL.getMsg());
        }
    }

    @GetMapping("islogin")
    @ApiOperation(value = "是否登录",notes = "是否登录")
    public ResponseWrap<Boolean> isLogin() throws ProjectException {
        try {
            Boolean flag = sellerService.isLogin();
            return ResponseWrap.<Boolean>builder()
                    .code(StatusEnum.SUCCEED.getCode())
                    .msg(StatusEnum.SUCCEED.getMsg())
                    .operationTime(new Date())
                    .data(flag)
                    .build();
        }catch (Exception e){
            log.error("是否登录：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(StatusEnum.ISLOGIN_FAIL.getCode(),
                    StatusEnum.ISLOGIN_FAIL.getMsg());
        }

    }

    @GetMapping("{sellerId}")
    @ApiOperation(value = "按ID商家用户信息",notes = "按ID商家用户信息")
    @ApiImplicitParam(paramType = "path",name = "sellerId",value = "商家ID",dataType = "Long")
    public ResponseWrap<SellerVo> findSellerById(@PathVariable("sellerId") Long sellerId) throws ProjectException {
        try {
            SellerVo sellerVo = SellerVo.builder().id(sellerId).build();
            SellerVo sellerVoById = sellerService.findSellerVoById(sellerVo);
            return ResponseWrap.<SellerVo>builder()
                    .code(StatusEnum.SUCCEED.getCode())
                    .msg(StatusEnum.SUCCEED.getMsg())
                    .operationTime(new Date())
                    .data(sellerVoById)
                    .build();
        }catch (Exception e){
            log.error("商家信息：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(StatusEnum.FAIL.getCode(),
                    StatusEnum.FAIL.getMsg());
        }
    }
}
